﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Globalization;
using System.Reflection;
using AkazoNobel.Scheduler.Core;
using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service;
using AkazoNobel.Ultimus.Extension;
using Dapper;
using Quartz;
using Ultimus.OC;
using Ultimus.WFServer;

namespace AkazoNobel.Scheduler.StoreMasterData.Tasks
{
    public class TintingDeviceDataSync : BaseTask
    {
        public override void ExecuteTask(IJobExecutionContext context)
        {
            var conn = SqlConnectionHelper.GetConnection(Constant.SMD_DB_NAME);
            var devices = SmdDeviceService.GetAllDevices();
            
            var dif = false;
            foreach (var device in devices)
            {
                var sql = "update t_smd_Device set 1=1";
                var logSql = "";
                var pcDevice = TintingService.GetTintingDeviceByFullsetsn(device.FullSetSN, "PC");
                var pcsn = pcDevice.Seq1 + " " + pcDevice.Seq2;
                if (pcsn != device.PcSN)
                {
                    dif = true;
                    sql += " ,pcsn = " + pcsn;
                    logSql += "insert into t_smd_StoreDataChangeLog (dataid,category,fieldname,beforevalue,aftervalue,changetime,UserID,assessmentid) " +
                              "values (" + device.DeviceID + ",'tinting','PcSN','" + device.PcSN + "','" + pcsn + "',getdate(),'magics11','20150625000001');";
                }
                var pcModel = pcDevice.Model;
                if (pcModel != device.PcModel)
                {
                    dif = true;
                    sql += " ,PcModel = " + pcModel;
                    logSql += "insert into t_smd_StoreDataChangeLog (dataid,category,fieldname,beforevalue,aftervalue,changetime,UserID,assessmentid) " +
                              "values (" + device.DeviceID + ",'tinting','PcModel','" + device.PcModel + "','" + pcModel + "',getdate(),'magics11','20150625000001');";
                }


                var shakerDevice = TintingService.GetTintingDeviceByFullsetsn(device.FullSetSN, "Shaker");
                var shakerSn = shakerDevice.Seq1 + " " + shakerDevice.Seq2;
                if (shakerSn != device.ShakerSN)
                {
                    dif = true;
                    sql += " ,pcsn = " + shakerSn;
                    logSql += "insert into t_smd_StoreDataChangeLog (dataid,category,fieldname,beforevalue,aftervalue,changetime,UserID,assessmentid) " +
                              "values (" + device.DeviceID + ",'tinting','Shaker','" + device.ShakerSN + "','" + shakerSn + "',getdate(),'magics11','20150625000001');";
                }


                var shakerModel = shakerDevice.Model;
                if (shakerModel != device.ShakerModel)
                {
                    dif = true;
                    sql += " ,ShakerModel = " + shakerModel;
                    logSql += "insert into t_smd_StoreDataChangeLog (dataid,category,fieldname,beforevalue,aftervalue,changetime,UserID,assessmentid) " +
                              "values (" + device.DeviceID + ",'ShakerModel','tinting','" + device.ShakerModel + "','" + shakerModel + "',getdate(),'magics11','20150625000001');";
                }


                var disDevice = TintingService.GetTintingDeviceByFullsetsn(device.FullSetSN, "Dispenser");
                var dissn = disDevice.Seq1 + " " + disDevice.Seq2;
                if (dissn != device.PcSN)
                {
                    dif = true;
                    sql += " ,pcsn = " + dissn;
                    logSql += "insert into t_smd_StoreDataChangeLog (dataid,category,fieldname,beforevalue,aftervalue,changetime,UserID,assessmentid) " +
                              "values (" + device.DeviceID + ",'DispenserSN','tinting','" + device.DispenserSN + "','" + dissn + "',getdate(),'magics11','20150625000001');";
                }
                var disModel = disDevice.Model;
                if (disModel != device.DispenserModel)
                {
                    dif = true;
                    sql += " ,DispenserModel = " + disModel;
                    logSql += "insert into t_smd_StoreDataChangeLog (dataid,category,fieldname,beforevalue,aftervalue,changetime,UserID,assessmentid) " +
                              "values (" + device.DeviceID + ",'tinting','DispenserModel','" + device.DispenserModel + "','" + disModel + "',getdate(),'magics11','20150625000001');";
                }

                sql += "where fullsetsn =" + device.FullSetSN;

                if (dif)
                {
                    conn.Execute(sql);
                    conn.Execute(logSql);
                }

            }
        }


    }
}
